package sis.studentinfo;

import static org.junit.Assert.assertEquals;

import java.math.BigDecimal;
import java.util.Date;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

import com.jimbob.ach.Ach;
import com.jimbob.ach.AchCredentials;
import com.jimbob.ach.AchResponse;
import com.jimbob.ach.AchStatus;
import com.jimbob.ach.AchTransactionData;

public class AccountTest {

	private Account account;
	public static final String ABA = "102000012";
	public static final String ACCOUNT_NUMBER = "194431518811";
	
	@Before
	public void init() {
		account = new Account();
		account.setBankAba(ABA);
		account.setBankAccountNumber(ACCOUNT_NUMBER);
		account.setBankAccountType(Account.BankAccountType.CHECKING);
	}
	
	@Test
	public void testTransferFromBank() {
		account.setAch(new Ach() {

			@Override
			public AchResponse issueDebit(AchCredentials credentials,
					AchTransactionData data) {
				Assert.assertTrue(data.account.equals(AccountTest.ACCOUNT_NUMBER));
				Assert.assertTrue(data.aba.equals(AccountTest.ABA));
				
				AchResponse response = new AchResponse();
				response.timestamp = new Date();
				response.traceCode = "1";
				response.status = AchStatus.SUCCESS;
				return response;
			}

			@Override
			public AchResponse markTransactionAsNSF(AchCredentials credentials,
					AchTransactionData data, String traceCode) {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public AchResponse refundTransaction(AchCredentials credentials,
					AchTransactionData data, String traceCode) {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public AchResponse issueCredit(AchCredentials credentials,
					AchTransactionData data, String traceCode) {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public AchResponse voidSameDayTransaction(
					AchCredentials credentials, AchTransactionData data,
					String traceCode) {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public AchResponse queryTransactionStatus(
					AchCredentials credentials, AchTransactionData data,
					String traceCode) {
				// TODO Auto-generated method stub
				return null;
			}
			
		});
		final BigDecimal amount = new BigDecimal("50.00");
		account.transferFromBank(amount);
		
		assertEquals(amount, account.getBalance());
	}
	
	@Test
	public void testTransactions() {
		account.credit(new BigDecimal("0.10"));
		account.credit(new BigDecimal("11.00"));
		assertEquals(new BigDecimal("11.10"), account.getBalance());
	}
	
	@Test
	public void testTransactionAverage() {
		account.credit(new BigDecimal("0.10"));
		account.credit(new BigDecimal("11.00"));
		account.credit(new BigDecimal("2.99"));
		assertEquals(new BigDecimal("4.70"), account.transactionAverage());
	}
	
}
